<?php
namespace Admin\Controller;

class AttributeController extends CommonController{
	public function model(){
		if(!$this->_model){
			$this->_model = D("Attribute");
		}
		return $this->_model;
	}
	public function add(){
		if(IS_GET){
			$Model = D("Type");
			$data = $Model->select();
			$this->assign("data",$data);
			$this->display();
		}else{
			$Model = D("Attribute");
			$data = $Model->create();
			if(!$data){
				$this->error($this->getError());
			}
			$res = $Model->add($data);
			if(!$res){
				$this->error("添加错误");
			}
			$this->success("数据添加成功",U("Attribute/Index"));
		}
	}
	public function index(){
		//属性的首页显示
		$Model = D("Attribute");
		$data = $Model->listDate();
		if(!$data){
			$this->error($this->getError());
		}
		$this->assign("type_data",$type_data);
		$this->assign("data",$data);
		$this->display();
	}
	public function dels(){
		$id = intval(I("get.id"));
		$Model = D("Attribute");
		$res = $Model->dels($id);
		if(!$res){
			$this->error("删除失败");
		}
		$this->success("删除成功",U("Attribute/Index"));
	}

	public function edit(){
		$Model = D("Attribute");
		if(IS_GET){
			$id = intval(I("get.id"));
			if($id <= 0){
				$this->error($this->getError());
			}
			$data = $Model->findOneById($id);

			$typeInfo = D("type")->select();
			if(!$data){
				$this->error("数据读取失败");
			}
			$this->assign("typeInfo",$typeInfo);
			$this->assign("data",$data);
			$this->display();
		}else{
			$data = $Model->create();
			$res = $Model->update($data);
			if(!$res){
				$this->error("修改失败");
			}
			$this->success("修改成功");
		}
	}

}